package com.xci.sys.service;


import com.github.pagehelper.Page;
import com.xci.core.helper.JsonHelper;
import com.xci.core.helper.Helper;
import com.xci.core.internal.Sys;
import com.xci.core.domain.HistoryInfo;
import com.xci.sys.dao.HistoryLogDao;
import com.xci.sys.entity.SysHistoryLog;
import com.xci.core.enums.HistoryOperateType;
import com.xci.sys.filter.HistoryLogFilter;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;

/**
 * 历史日志服务
 */
@Service
public class HistoryLogService {
    @Resource
    private HistoryLogDao historyDao;//历史记录数据层对象

    /**
     * 保存新建历史日志
     *
     * @param history 历史日志对象
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveInsert(HistoryInfo history) {
        SysHistoryLog entity = buildHistory(history, HistoryOperateType.Insert);
        historyDao.insert(entity);
    }

    /**
     * 保存更新历史日志
     *
     * @param history 历史日志对象
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveUpdate(HistoryInfo history) {
        SysHistoryLog entity = buildHistory(history, HistoryOperateType.Update);
        historyDao.insert(entity);
    }

    /**
     * 保存删除历史日志
     *
     * @param history 历史日志对象
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveDelete(HistoryInfo history) {
        SysHistoryLog entity = buildHistory(history, HistoryOperateType.Delete);
        historyDao.insert(entity);
    }

    /**
     * 查询单个历史日志
     *
     * @param id 历史日志主键
     * @return 返回历史日志对象
     */
    public SysHistoryLog selectById(@NotBlank(message = "请指定历史日志主键") String id) {
        return historyDao.selectById(id);
    }

    /**
     * 查询历史日志分页列表
     *
     * @param filter 分页过滤对象
     * @return 返回历史日志分页列表
     */
    public Page<SysHistoryLog> selectPageList(HistoryLogFilter filter) {
        filter.splitCategory();
        return historyDao.selectPageList(filter);
    }

    /**
     * 生成历史日志对象
     */
    private SysHistoryLog buildHistory(HistoryInfo history, HistoryOperateType historyOperateType) {
        SysHistoryLog entity = new SysHistoryLog();
        entity.setId(Helper.nextIdStr());
        entity.setPrimaryKey(history.getPk().toString());
        entity.setBeforeData(JsonHelper.serialize(history.getBefore(), true));
        entity.setAfterData(JsonHelper.serialize(history.getAfter(), true));
        Sys.me().setUserInfo(entity, false);

        //1新建 2修改 3删除
        switch (historyOperateType) {
            case Insert:
                entity.setCategory(1);
                if (history.getAfter() != null) {
                    entity.setTableName(history.getAfter().getClass().getSimpleName());
                }
                break;
            case Update:
                entity.setCategory(2);
                if (history.getBefore() != null) {
                    entity.setTableName(history.getBefore().getClass().getSimpleName());
                }
                if (entity.getTableName() != null && history.getAfter() != null) {
                    entity.setTableName(history.getAfter().getClass().getSimpleName());
                }
                if (history.getBefore() != null && history.getAfter() != null) {
                    String msg = Helper.contrast(history.getBefore(), history.getAfter());
                    entity.setRemark(msg);
                }
                break;
            case Delete:
                entity.setCategory(3);
                if (history.getBefore() != null) {
                    entity.setTableName(history.getBefore().getClass().getSimpleName());
                }
                break;
        }
        if (Helper.isBlank(entity.getTableName())) {
            entity.setTableName("null");
        }
        if (Helper.isBlank(entity.getPrimaryKey())) {
            entity.setPrimaryKey("0");
        }
        return entity;
    }
}